package cn.safekeeper.guide.admin.controller.system;

import cn.safekeeper.core.SafeKeeper;
import cn.safekeeper.guide.admin.constant.Constants;
import cn.safekeeper.guide.admin.constant.MsgObj;
import cn.safekeeper.guide.admin.constant.UserConstants;
import cn.safekeeper.guide.admin.controller.BaseController;
import cn.safekeeper.guide.admin.domain.entity.SysUserRole;
import cn.safekeeper.guide.admin.domain.entity.SysRole;
import cn.safekeeper.guide.admin.domain.entity.SysUser;
import cn.safekeeper.guide.admin.domain.page.TableDataInfo;
import cn.safekeeper.guide.admin.service.ISysRoleService;
import cn.safekeeper.guide.admin.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色信息
 */
@RestController
@RequestMapping("/system/role")
public class SysRoleController extends BaseController {

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysUserService userService;

    @GetMapping("/list")
    public TableDataInfo list(SysRole role) {
        startPage();
        List<SysRole> list = roleService.selectRoleList(role);
        return getDataTable(list);
    }


    /**
     * 根据角色编号获取详细信息
     */
    @GetMapping("/query")
    public MsgObj getInfo(Long roleId) {
        if(roleId == null){
            return error("角色ID为空");
        }
        return MsgObj.success(roleService.selectRoleById(roleId));
    }

    /**
     * 新增角色
     */
    @PostMapping("/add")
    public MsgObj add(@Validated @RequestBody SysRole role) {
        if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
            return MsgObj.error("新增角色'" + role.getRoleName() + "'失败，角色名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
            return MsgObj.error("新增角色'" + role.getRoleName() + "'失败，角色权限已存在");
        }
        role.setCreateBy(getUsername());
        return toAjax(roleService.insertRole(role));

    }

    /**
     * 修改保存角色
     */
    @PutMapping("/edit")
    public MsgObj edit(@Validated @RequestBody SysRole role) {
        roleService.checkRoleAllowed(role);
        if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
            return MsgObj.error("修改角色'" + role.getRoleName() + "'失败，角色名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
            return MsgObj.error("修改角色'" + role.getRoleName() + "'失败，角色权限已存在");
        }
        role.setUpdateBy(getUsername());

        if (roleService.updateRole(role) > 0) {
            // 更新缓存用户权限
            SafeKeeper.safeDefault().clearRoleOrPermissionCacheData(getUserId(), Constants.PERMISSION_CACHE_MARK);
            return MsgObj.success();
        }
        return MsgObj.error("修改角色'" + role.getRoleName() + "'失败，请联系管理员");
    }

    /**
     * 修改保存数据权限
     */
    @PutMapping("/dataScope")
    public MsgObj dataScope(@RequestBody SysRole role) {
        roleService.checkRoleAllowed(role);
        return toAjax(roleService.authDataScope(role));
    }

    /**
     * 状态修改
     */
    @PutMapping("/changeStatus")
    public MsgObj changeStatus(@RequestBody SysRole role) {
        roleService.checkRoleAllowed(role);
        role.setUpdateBy(getUsername());
        return toAjax(roleService.updateRoleStatus(role));
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/remove")
    public MsgObj remove(Long[] roleId) {
        return toAjax(roleService.deleteRoleByIds(roleId));
    }

    /**
     * 获取角色选择框列表
     */
    @GetMapping("/optionselect")
    public MsgObj optionselect() {
        return MsgObj.success(roleService.selectRoleAll());
    }

    /**
     * 查询已分配用户角色列表
     */
    @GetMapping("/authUser/query")
    public TableDataInfo allocatedList(SysUser user) {
        startPage();
        List<SysUser> list = userService.selectAllocatedList(user);
        return getDataTable(list);
    }

    /**
     * 查询未分配用户角色列表
     */
    @GetMapping("/authUser/edit")
    public TableDataInfo unallocatedList(SysUser user) {
        startPage();
        List<SysUser> list = userService.selectUnallocatedList(user);
        return getDataTable(list);
    }

    /**
     * 取消授权用户
     */
    @PutMapping("/authUser/cancel")
    public MsgObj cancelAuthUser(@RequestBody SysUserRole userRole) {
        return toAjax(roleService.deleteAuthUser(userRole));
    }

    /**
     * 批量取消授权用户
     */
    @PutMapping("/authUser/cancelAll")
    public MsgObj cancelAuthUserAll(Long roleId, Long[] userIds) {
        return toAjax(roleService.deleteAuthUsers(roleId, userIds));
    }

    /**
     * 批量添加用户授权
     */
    @PutMapping("/authUser/add")
    public MsgObj selectAuthUserAll(Long roleId, Long[] userIds) {
        return toAjax(roleService.insertAuthUsers(roleId, userIds));
    }
}
